package com.by.toolkitspring.support;

import com.google.common.collect.Lists;
import com.by.toolkit.enums.WxCpAddWayEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Slf4j
public class FilterWxCpAddMethodSupport {


    public static final Predicate<FilterInfo> SCAN_QRCODE_FILTER = r -> WxCpAddWayEnum.SCAN_QRCODE.getCode().toString().equals(r.getAddWay()) && StringUtils.isBlank(r.getState());
    public static final Predicate<FilterInfo> DYNAMIC_QRCODE_FILTER = r -> WxCpAddWayEnum.SCAN_QRCODE.getCode().toString().equals(r.getAddWay()) && StringUtils.isNotBlank(r.getState());
    public static final Predicate<FilterInfo> CUSTOMER_ACQUISITION_LINK_FILTER = r -> WxCpAddWayEnum.CUSTOMER_ACQUISITION_LONK.getCode().toString().equals(r.getAddWay()) && StringUtils.isNotBlank(r.getState());
    public static final Predicate<FilterInfo> MINGPIAN_FILTER = r -> WxCpAddWayEnum.MING_PIAN.getCode().toString().equals(r.getAddWay());
    public static final Predicate<FilterInfo> ADMIN_DIVIDE_FILTER = r -> WxCpAddWayEnum.ADMIN_DIVIDE.getCode().toString().equals(r.getAddWay());

    public static final Predicate<FilterInfo> OTHER_FILTER = r ->
            !SCAN_QRCODE_FILTER.test(r)
            && !DYNAMIC_QRCODE_FILTER.test(r)
            && !CUSTOMER_ACQUISITION_LINK_FILTER.test(r)
            && !MINGPIAN_FILTER.test(r)
            && !ADMIN_DIVIDE_FILTER.test(r)
            ;

    private static final List<Predicate<FilterInfo>> FRIEND_METHOD_FILTERS = Lists.newArrayList(
            FilterWxCpAddMethodSupport.DYNAMIC_QRCODE_FILTER,
            FilterWxCpAddMethodSupport.CUSTOMER_ACQUISITION_LINK_FILTER,
            FilterWxCpAddMethodSupport.SCAN_QRCODE_FILTER,
            FilterWxCpAddMethodSupport.MINGPIAN_FILTER,
            FilterWxCpAddMethodSupport.ADMIN_DIVIDE_FILTER,
            FilterWxCpAddMethodSupport.OTHER_FILTER
    );

    private static final Map<Predicate<FilterInfo>, WxCpAddWayEnum> FILTER_FRIEND_METHOD_MAP = Map.of(
            FilterWxCpAddMethodSupport.DYNAMIC_QRCODE_FILTER, WxCpAddWayEnum.DYNAMIC_QRCODE,
            FilterWxCpAddMethodSupport.CUSTOMER_ACQUISITION_LINK_FILTER, WxCpAddWayEnum.CUSTOMER_ACQUISITION_LONK,
            FilterWxCpAddMethodSupport.SCAN_QRCODE_FILTER, WxCpAddWayEnum.SCAN_QRCODE,
            FilterWxCpAddMethodSupport.MINGPIAN_FILTER, WxCpAddWayEnum.MING_PIAN,
            FilterWxCpAddMethodSupport.ADMIN_DIVIDE_FILTER, WxCpAddWayEnum.ADMIN_DIVIDE,
            FilterWxCpAddMethodSupport.OTHER_FILTER, WxCpAddWayEnum.OTHER
    );

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static final class FilterInfo {

        private String addWay;

        private String state;

        private Integer delFlag;

    }

    public static WxCpAddWayEnum mapFriendWay(List<FilterInfo> filterInfos){
        List<WxCpAddWayEnum> friendMethods = filterInfos.stream()
                .map(i -> FRIEND_METHOD_FILTERS.stream()
                        .filter(f -> f.test(i))
                        .map(FILTER_FRIEND_METHOD_MAP::get)
                        .findFirst()
                        .orElse(WxCpAddWayEnum.OTHER))
                .collect(Collectors.toList());
        if (friendMethods.contains(WxCpAddWayEnum.DYNAMIC_QRCODE)) {
            return WxCpAddWayEnum.DYNAMIC_QRCODE;
        }
        return friendMethods.get(0);
    }

}
